package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import butterknife.BindView;
import com.supply.dl.R;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.SearchStockBean;
import com.supply.dl.bean.SelectDictionaryBean;
import com.supply.dl.bean.WorkNotesBean;
import com.supply.dl.bean.request.SaveChangeCuLbs;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.StringUtils;

import java.util.ArrayList;

/**
 * 库位转移
 */
public class RepertoryChangeActivity extends BaseActivity
{
	private ArrayList<WorkNotesBean> remarks = new ArrayList<>();
	@BindView(R.id.txt_ck) TextView txtCu;
	@BindView(R.id.txt_ck_lbs) TextView txtCuLbs;
	@BindView(R.id.txt_wu_info) TextView txtWInfo;
	@BindView(R.id.txt_change_lbs) TextView txtChangeLbs;
	@BindView(R.id.txt_change_ck) TextView txtChangeCu;
	@BindView(R.id.edit_num_jian) EditText editWuNum;
	@BindView(R.id.edit_num_dun) EditText editWeight;
	@BindView(R.id.edit_num_fang) EditText editFang;
	@BindView(R.id.edit_why) EditText editWhy;
	@BindView(R.id.txt_save) TextView txtSave;
	@BindView(R.id.txt_select_work_notes) TextView txtWorkNotes;
	@BindView(R.id.layout_after_select_cu) LinearLayout layoutAfter;
	/**
	 * 进度框
	 */
	private ProgressBar progressBar;

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_repertory_change;
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitleShow(getResourcesString(R.string.library_transfer));
		showLeftBack();
		txtCu.setOnClickListener(this);
		txtCuLbs.setOnClickListener(this);
		txtChangeLbs.setOnClickListener(this);
		//txtChangeCu.setOnClickListener(this);
		txtSave.setOnClickListener(this);
		txtWorkNotes.setOnClickListener(this);
	}

	private String skuId;
	private SelectDictionaryBean beforeBeanCu, afterBeanCu, afterBeanCuLbs;
	private SearchStockBean beforeBeanCuLbs;
	public static final int BEFORE_CK = 101;
	public static final int AFTER_CK = 102;
	public static final int BEFORE_CK_LBS = 103;
	public static final int AFTER_CK_LBS = 104;
	public static final int SELECT_WORK_NOTE = 105;

	@Override public void onClick(View v)
	{
		super.onClick(v);
		if (v == txtCuLbs)
		{
			if (beforeBeanCu == null)
			{
				showToast("请先选择仓库");
				return;
			}
			Intent intent = new Intent(this, DepotSelectActivity.class);
			intent.putExtra("whId", beforeBeanCu.getValue());
			startActivityForResult(intent, BEFORE_CK_LBS);
		}
		else if (v == txtCu)
		{
			Intent intent = new Intent(this, SelectBaseDataActivity.class);
			intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_WAREHOUSE);
			startActivityForResult(intent, BEFORE_CK);
		}
		else if (v == txtChangeLbs)
		{
			if (beforeBeanCu == null)
			{
				showToast("请先选择仓库");
				return;
			}
//			Intent intent = new Intent(this, DepotSelectActivity.class);
//			intent.putExtra("whId", beforeBeanCu.getValue());
//			startActivityForResult(intent, AFTER_CK_LBS);
			Intent intent = new Intent(this, SelectBaseDataActivity.class);
			intent.putExtra("action", AppConfig.BaseDataAction.WARE_SELECT_STOCK_POSITION);
			intent.putExtra("whId", beforeBeanCu.getValue());
			startActivityForResult(intent, AFTER_CK_LBS);
		}
//		else if (v == txtChangeCu)
//		{
//			Intent intent = new Intent(this, SelectBaseDataActivity.class);
//			intent.putExtra("action", AppConfig.BaseDataAction.WARE_HOUSE_TYPE_0);
//			startActivityForResult(new Intent(this, SelectBaseDataActivity.class), AFTER_CK);
//		}
		else if (v == txtSave)
		{
			saveChangeLbs();
		}
		else if (v == txtWorkNotes)
		{
			Intent intent = new Intent(this, OperateRemarkActivity.class);
			startActivityForResult(intent, SELECT_WORK_NOTE);
		}
	}

	@Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null)
		{
			switch (requestCode)
			{
			case BEFORE_CK:
				beforeBeanCu = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				if (beforeBeanCu == null)
					return;
				txtCu.setText(beforeBeanCu.getText());
				txtChangeCu.setText(beforeBeanCu.getText());
				break;
			case BEFORE_CK_LBS:
				beforeBeanCuLbs = (SearchStockBean) data.getSerializableExtra("selectBean");
				if (beforeBeanCuLbs == null)
				{
					layoutAfter.setVisibility(View.GONE);
					return;
				}
				layoutAfter.setVisibility(View.VISIBLE);
				txtCuLbs.setText(beforeBeanCuLbs.getStockName());
				String wulInfo = "物料编码：" + beforeBeanCuLbs.getSkuCode() + "\n" + "物料名称：" + beforeBeanCuLbs.getSkuName() + "\n"
						+ "库存类型：" + beforeBeanCuLbs.getStockTypeName() + "\n" + "库存状态：" + beforeBeanCuLbs.getStockStatusName() + "\n"
						+ "批次号：" + beforeBeanCuLbs.getBatchNo() + "\n" + "数量(件)：" + beforeBeanCuLbs.getAmount() + "\n"
						+ "数量(吨)：" + beforeBeanCuLbs.getWeight() + "\n"+ "数量(方)：" + beforeBeanCuLbs.getVolume();
				txtWInfo.setText(wulInfo);

				editWuNum.setText(beforeBeanCuLbs.getAmount());
				editWeight.setText(beforeBeanCuLbs.getWeight());
				editFang.setText(beforeBeanCuLbs.getVolume());
				break;
//			case AFTER_CK:
//				afterBeanCu = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
//				if (afterBeanCu == null)
//					return;
//				txtChangeCu.setText(afterBeanCu.getText());
//				break;
			case AFTER_CK_LBS:
				afterBeanCuLbs = (SelectDictionaryBean) data.getSerializableExtra("selectBean");
				if (afterBeanCuLbs == null)
					return;
				txtChangeLbs.setText(afterBeanCuLbs.getText());
				break;
			case SELECT_WORK_NOTE:
				remarks = (ArrayList<WorkNotesBean>) data.getSerializableExtra("remarks");
				if (remarks.size() > 0)
				{
					String workNoteName = "";
					for (int i = 0; i < remarks.size(); i++)
					{
						if (StringUtils.isBlank(workNoteName))
							workNoteName = remarks.get(i).getDepart();
						else
							workNoteName = workNoteName + "," + remarks.get(i).getDepart();
					}
					txtWorkNotes.setText(workNoteName + "");
				}
				break;
			}
		}
	}

	/**
	 * 保存库位转移信息
	 */
	private void saveChangeLbs()
	{
		if (afterBeanCuLbs == null) {
			showToast("请选择将要转移的库位");
			return;
		}
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);

		SaveChangeCuLbs transferData = new SaveChangeCuLbs();
		//设置相关参数
		transferData.setInvId(beforeBeanCuLbs.getId());
		transferData.setAmount(beforeBeanCuLbs.getAmount());
		transferData.setWeight(beforeBeanCuLbs.getWeight());
		transferData.setVolume(beforeBeanCuLbs.getVolume());
		transferData.setStockId(beforeBeanCuLbs.getStockId());
		transferData.setTraAmount(editWuNum.getText().toString().trim());
		transferData.setTraWeight(editWeight.getText().toString().trim());
		transferData.setTraVolume(editFang.getText().toString().trim());
		transferData.setTraRemark(editWhy.getText().toString().trim());
		transferData.setTraStockId(afterBeanCuLbs.getValue());
		transferData.setJobAmountList(remarks);
		httpManger.saveChangeLbs(transferData);
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{
				if (baseInfo.isHandleState())
				{
					RepertoryChangeActivity.this.finish();
					showToast(baseInfo.getMessage());
				}
			}

			@Override public void onFailure(String message)
			{
				showToast(message);
			}
		});
	}
}
